'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview VIP特权礼包列表
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var dataApi = require('../../../../libs/dataApi');
var vipShopConf = require('../../../../config/vipShopConf');
var PlayerManager = require('../../../../libs/playerManager');
var Code = require('../../../../consts/code');

module.exports = function (msg, session, next) {
    var role_id = session.uid;

    var shop_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'shop', 'discount', 'vip', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var vip_model = player_manager.vip;
                var discount_model = player_manager.discount;
                var discount_shop_num_list = discount_model.discout_shop_num_list;

                var discount_conf_list = dataApi.discount_shop.all();
                _.each(discount_conf_list, function (diff_item) {
                    var shop_id = diff_item[0].id;
                    var shop_num = discount_model.getDiscountShopNum(shop_id);
                    var total_num = diff_item[0].buy_times;
                    shop_list.push({
                        shop_id: shop_id,
                        shop_num: total_num - shop_num
                    });
                });
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    shop_list: shop_list
                }
            });
        }
    );
};
